package LeetCode.leetcode135;

import java.util.Arrays;

class Solution {
    public int candy(int[] ratings) {
        if (ratings == null && ratings.length < 1)
            return -1;
        int[] resArr = new int[ratings.length];
        resArr[0] = 1;
        for (int i = 1; i < ratings.length; i++) {
            if (ratings[i] > ratings[i - 1])
                resArr[i] = resArr[i - 1] + 1;
            else
                resArr[i] = 1;
        }
        for (int i = resArr.length - 2; i >= 0; i--) {
            if (ratings[i] > ratings[i + 1]) {
                resArr[i] = Math.max(resArr[i], resArr[i + 1] + 1);
            }
        }
        int res = 0;
        for (int item : resArr) {
            res += item;
        }
        return res;
    }
}